package cz.semjob.learning.classifier.benchmark;

import java.util.ArrayList;
import java.util.List;

import weka.classifiers.Classifier;
import cz.semjob.document.annotation.LabeledWords;
import cz.semjob.document.annotation.metadata.Entity;
import cz.semjob.document.util.DocumentUtil;
import cz.semjob.learning.instances.creator.ITextInstanceCreator;

public class ClassifierEntityBenchmarkResults extends
		ClassifierBenchmarkResults {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6091646221716889309L;

	private Entity entity;

	private ITextInstanceCreator textInstanceCreator;

	private List<ClassifierEntityBenchmarkResult> documentResults;

	public ClassifierEntityBenchmarkResults() {
		super();
	}

	public ClassifierEntityBenchmarkResults(Entity entity,
			Classifier classifier, ITextInstanceCreator textInstanceCreator,
			String classifierInfo) {
		super();
		this.entity = entity;
		this.documentResults = new ArrayList<ClassifierEntityBenchmarkResult>();
		this.classifier = classifier;
		this.textInstanceCreator = textInstanceCreator;
		this.classifierInfo = classifierInfo;
	}

	public Entity getEntity() {
		return entity;
	}

	public void setEntity(Entity entity) {
		this.entity = entity;
	}

	public void addResult(String documentName, double precision, double recall,
			double trueNegativeRate, double accuracy,
			List<LabeledWords> foundWords) {
		this.documentResults.add(new ClassifierEntityBenchmarkResult(
				documentName, precision, recall, trueNegativeRate, accuracy,
				foundWords));
	}

	public ITextInstanceCreator getInstanceCreator() {
		return textInstanceCreator;
	}

	public String toPrettyString() {
		StringBuilder sb = new StringBuilder();

		sb.append("ClassifierBenchmarkResults for entity: " + entity.getName()
				+ "\n");
		sb.append(this.overallResult.toString() + "\n");
		sb.append(this.dataSetStatistics.toString() + "\n");
		sb.append("\n");
		for (ClassifierEntityBenchmarkResult result : this.documentResults) {
			sb.append(result.toString());
			for (LabeledWords resultWords : result.getFoundWords()) {
				sb.append(DocumentUtil.printWords(resultWords.getWords())
						+ "\n");
			}
			sb.append("\n");
		}
		sb.append("\n");
		return sb.toString();
	}

	@Override
	public String toInfoString() {
		StringBuilder info = new StringBuilder();
		info.append("entity=" + entity.getName() + "\n");
		info.append("classifier=" + this.classifierInfo + "\n");
		info.append("accuracy=" + this.overallResult.getAccuracy() + "\n");
		info.append("precision=" + this.overallResult.getPrecision() + "\n");
		info.append("recall=" + this.overallResult.getRecall() + "\n");
		info.append("fmeasure=" + this.overallResult.getFMeasure() + "\n");
		return info.toString();
	}
}
